package com.zxn.dorm.view.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zxn.dorm.R;
import com.zxn.dorm.view.ISPKey;
import com.zxn.expandapp.widget.ClearEditText;
import com.zxn.presenter.view.BaseActivity;
import com.zxn.titleview.TitleView;
import com.zxn.utils.SystemSPUtil;
import com.zxn.utils.UIUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 新版的登陆
 */
public class LoginActivity extends BaseActivity {

    @BindView(R.id.title_common)
    TitleView titleCommon;
    @BindView(R.id.edit_phone)
    ClearEditText mEtName;
    @BindView(R.id.edit_pwd)
    ClearEditText mEtPwd;
    //    @BindView(R.id.cb_savepwd)
//    CheckBox img_check;
    @BindView(R.id.register)
    TextView register;
    @BindView(R.id.login)
    TextView mTvLogin;
    @BindView(R.id.tv_agreement)
    TextView tvAgreement;
    @BindView(R.id.parent)
    RelativeLayout parent;
    @BindView(R.id.cb_savepwd)
    CheckBox cbSavepwd;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_login;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        cbSavepwd.setOnCheckedChangeListener((compoundButton, b) -> {
            SystemSPUtil.saveData(mContext, ISPKey.KEY_SAVE, b);

        });

        boolean isSavePw = (boolean) SystemSPUtil.getData(mContext, ISPKey.KEY_SAVE, false);
        cbSavepwd.setChecked(isSavePw);
        if (cbSavepwd.isChecked()) {
            String name = (String) SystemSPUtil.getData(mContext, ISPKey.KEY_USER_NAME, "");
            String pw = (String) SystemSPUtil.getData(mContext, ISPKey.KEY_PASS_WORD, "");
            mEtName.setText(name);
            mEtPwd.setText(pw);
        }
    }

    @OnClick({R.id.login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.login:
                String userName = mEtName.getText().toString().trim();
                String passWord = mEtPwd.getText().toString().trim();
                login(userName, passWord);
                break;
        }
    }

    private void login(String userName, String passWord) {
        if (UIUtils.isEmpty(userName)) {
            showToast("请输入用户名");
            return;
        }

        if (UIUtils.isEmpty(passWord)) {
            showToast("请输入密码");
            return;
        }

        if (userName.equals("1714184216") && passWord.equals("123")) {
            showToast("登录成功!");
            boolean isSavePw = (boolean) SystemSPUtil.getData(mContext, ISPKey.KEY_SAVE, false);
            if (isSavePw) {
                SystemSPUtil.saveData(mContext, ISPKey.KEY_USER_NAME, userName);
                SystemSPUtil.saveData(mContext, ISPKey.KEY_PASS_WORD, passWord);
            }
            DormListActivity.jumpTo(mContext);
            finish();
        } else {
            showToast("登录失败,用户名或密码错!");
        }
    }

}
